<% set_title(t(".title", :id => @changeset.id)) %>

<%= render "sidebar_header", :title => t(".title", :id => @changeset.id) %>

<div class="browse-section">
  <p class="fs-6 overflow-x-auto" dir="auto">
    <%= linkify(@changeset.tags["comment"].to_s.presence || t("browse.no_comment")) %>
  </p>
  <%= tag.p :class => "details", :data => { :changeset => changeset_data(@changeset) } do %>
    <%= changeset_details(@changeset) %>
  <% end %>

  <%= render :partial => "browse/tag_details", :object => @changeset.tags.except("comment") %>

  <div class="row">
    <div class="col">
      <h4><%= t(".discussion") %></h4>
    </div>

    <% if current_user %>
      <div class="col-auto">
        <% if @changeset.subscribers.exists?(current_user.id) %>
          <%= tag.button t(".unsubscribe"),
                         :class => "btn btn-sm btn-primary",
                         :name => "unsubscribe",
                         :data => { :method => "DELETE",
                                    :url => api_changeset_subscription_path(@changeset) } %>
        <% else %>
          <%= tag.button t(".subscribe"),
                         :class => "btn btn-sm btn-primary",
                         :name => "subscribe",
                         :data => { :method => "POST",
                                    :url => api_changeset_subscription_path(@changeset) } %>
        <% end %>
      </div>
    <% end %>
  </div>

  <% @comments.each do |comment| %>
    <% next unless comment.visible || current_user&.moderator? %>
    <article id="c<%= comment.id %>">
      <small class='text-body-secondary'>
        <%= comment_by_options = { :time_ago => friendly_date_ago(comment.created_at),
                                   :user => link_to(comment.author.display_name, comment.author) }
            comment.visible ? t(".comment_by_html", **comment_by_options) : t(".hidden_comment_by_html", **comment_by_options) %>
        <% if current_user&.moderator? %>
          —
          <%= tag.button t(".#{comment.visible ? 'hide' : 'unhide'}_comment"),
                         :class => "btn btn-sm small btn-link link-secondary p-0 align-baseline",
                         :data => { :method => comment.visible ? "DELETE" : "POST",
                                    :url => api_changeset_comment_visibility_path(comment) } %>
        <% end %>
        <a href="#c<%= comment.id %>">
          <svg width="16" height="16" fill="currentColor">
            <path d="M4.715 6.542 3.343 7.914a3 3 0 1 0 4.243 4.243l1.828-1.829A3 3 0 0 0 8.586 5.5L8 6.086a1 1 0 0 0-.154.199 2 2 0 0 1 .861 3.337L6.88 11.45a2 2 0 1 1-2.83-2.83l.793-.792a4 4 0 0 1-.128-1.287z" />
            <path d="M6.586 4.672A3 3 0 0 0 7.414 9.5l.775-.776a2 2 0 0 1-.896-3.346L9.12 3.55a2 2 0 1 1 2.83 2.83l-.793.792c.112.42.155.855.128 1.287l1.372-1.372a3 3 0 1 0-4.243-4.243z" />
          </svg>
        </a>
      </small>
      <div class="mx-2">
        <%= comment.body.to_html %>
      </div>
    </article>
  <% end %>

  <% unless current_user %>
    <p>
      <%= link_to(t(".join_discussion"), login_path(:referer => request.fullpath)) %>
    </p>
  <% end %>

  <% if current_user %>
    <% unless @changeset.open? %>
      <form action="#" class="mb-3">
        <div class="mb-3">
          <textarea class="form-control" name="text" cols="40" rows="5"></textarea>
        </div>
        <div id="comment-error" class="alert alert-danger p-2 mb-3" hidden>
        </div>
        <div>
          <%= tag.button t(".comment"),
                         :class => "btn btn-primary",
                         :name => "comment",
                         :disabled => true,
                         :data => { :method => "POST",
                                    :url => api_changeset_changeset_comments_path(@changeset) } %>
        </div>
      </form>
    <% else %>
      <p>
        <%= t(".still_open") %>
      </p>
    <% end %>
  <% end %>

  <% unless @ways.empty? %>
    <%= render :partial => "elements", :locals => { :type => "way", :elements => @ways, :pages => @way_pages } %>
  <% end %>

  <% unless @relations.empty? %>
    <%= render :partial => "elements", :locals => { :type => "relation", :elements => @relations, :pages => @relation_pages } %>
  <% end %>

  <% unless @nodes.empty? %>
    <%= render :partial => "elements", :locals => { :type => "node", :elements => @nodes, :pages => @node_pages } %>
  <% end %>
</div>

<div class='secondary-actions'>
  <%= link_to t(".changesetxml"), api_changeset_path(@changeset) %>
  &middot;
  <%= link_to t(".osmchangexml"), api_changeset_download_path(@changeset) %>
</div>

<% if @next_by_user || @prev_by_user %>
  <div class='secondary-actions'>
    <% if @prev_by_user %>
      <%= link_to @prev_by_user, :class => "icon-link" do %>
        <%= previous_page_svg_tag :height => 11 %>
        <%= @prev_by_user.id %>
      <% end %>
      &middot;
    <% end %>
    <%= user = (@prev_by_user || @next_by_user).user.display_name
        link_to tag.bdi(user), :controller => "changesets", :action => "index", :display_name => user %>
    <% if @next_by_user %>
      &middot;
      <%= link_to @next_by_user, :class => "icon-link" do %>
        <%= @next_by_user.id %>
        <%= next_page_svg_tag :height => 11 %>
      <% end %>
    <% end %>
  </div>
<% end %>
